package com.example.controller;

import com.example.algorithm.CapacityEvalAlgorithm;
import com.example.entity.*;
import com.example.service.SafeIntervalAndCapacityAccessService;
import com.example.util.MicroResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @program: microservice
 * @description:
 * @author: Aprilies
 * @create: 2024-09-10 16:50
 **/

@RestController
public class SafeIntervalAndCapacityAccessController {
    @Autowired
    private SafeIntervalAndCapacityAccessService safeIntervalAndCapacityAccessService;

    ///////////////////////// 航路信息Air Lane的增删改查、分页、容量评估、安全间隔查询  下方 //////////////////////////////////////
    @GetMapping("/airlane/selectall")
    public ResponseEntity getAirlaneAllUsers() {
        return ResponseEntity.ok(MicroResponseUtils.success(safeIntervalAndCapacityAccessService.findAllAirLanes()));
    }

    @GetMapping("/airlane/select")
    public ResponseEntity getAirlaneUserById(@RequestParam Long id) {
        Optional<AirLaneEntity> user = safeIntervalAndCapacityAccessService.findAirLaneById(id);
        ResponseEntity responseEntity;
        if(user.isPresent()){
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(user.get()));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @PostMapping("/airlane/create")
    public ResponseEntity createAirlaneUser(@RequestBody AirLaneEntity user) {
        AirLaneEntity createdUser = safeIntervalAndCapacityAccessService.saveAirLane(user);
        return ResponseEntity.ok(MicroResponseUtils.success(null));
    }

    @PostMapping("/airlane/update")
    public ResponseEntity updateAirlaneUser(@RequestBody AirLaneEntity user) {
        Optional<AirLaneEntity> update = safeIntervalAndCapacityAccessService.updateAirLane(user);
        ResponseEntity responseEntity;
        if(update.isPresent()){
            responseEntity = new ResponseEntity<>(update.get(), HttpStatus.OK);
            //return ResponseEntity.ok(MicroResponseUtils.success(update.get()));
        }
        else{
            responseEntity = new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
            //return ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @PostMapping("/airlane/delete")
    public ResponseEntity deleteAirlaneUser(@RequestParam Long id) {
        //return userSysService.deleteAirLane(id) ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
        boolean isDeleted = safeIntervalAndCapacityAccessService.deleteAirLane(id);
        return isDeleted ? ResponseEntity.ok(MicroResponseUtils.success("User deleted successfully")) : ResponseEntity.ok(MicroResponseUtils.error("User not found"));
    }

    @PostMapping("/airlane/capacityeval")
    public ResponseEntity AirlaneCapacityEval(@RequestBody AirLaneEntity user) {
        // 调出user对象中的关键参数
        String dataSource_temp = user.getDataSource();
        BigDecimal airspaceRange_temp = user.getAirspaceRange();
        Integer min15CapacityEvalResult_temp = user.getMin15CapacityEvalResult();
        Integer h1CapacityEvalResult_temp = user.getH1CapacityEvalResult();
        Integer staticCapacityEvalResult_temp = user.getStaticCapacityEvalResult();

        // 此处运用上述输入的关键参数，运行容量评估算法，得到最终要输入给前端的  capacityEvalResult
        CapacityEvalAlgorithm capacityEvalAlgorithm = new CapacityEvalAlgorithm();
        Integer[] capacityEvalResult = capacityEvalAlgorithm.capacityEval();
        // 得到待评估的容量评估值
        user.setMin15CapacityEvalResult(capacityEvalResult[0]);
        user.setH1CapacityEvalResult(capacityEvalResult[1]);
        user.setStaticCapacityEvalResult(capacityEvalResult[2]);

        // 将容量评估值和HTTP响应一同返回
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @GetMapping("/airlane/idsafeinterval")
    public ResponseEntity getAirlaneSafeInterval(@RequestParam Long id) {
        Optional<AirLaneEntity> user = safeIntervalAndCapacityAccessService.findAirLaneById(id); // 获得某id的所有数据
        ResponseEntity responseEntity;
        if(user.isPresent()){
            String SafeIntervalRaw = user.get().getSafeIntervalRepo();    // 获得列表中的字符串
            Integer[][][] safeinterval_output = SafeIntervalAndCapacityAccessService.Stringto3Array(SafeIntervalRaw);
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(safeinterval_output));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @GetMapping("/airlane/idsafeintervalall")
    public ResponseEntity getAirlaneSafeIntervalall() {
        List<AirLaneEntity> user = safeIntervalAndCapacityAccessService.findAllAirLanes(); // 获得某id的所有数据
        ResponseEntity responseEntity;
        List<SafeIntervalEntity> safeIntervalEntityList = new ArrayList<>();

        for (int i = 0; i < 20; i++){
            AirLaneEntity airlane = user.get(i);
            Integer id = airlane.getId();
            String SafeIntervalRaw = airlane.getSafeIntervalRepo();
            Integer[][][] safeinterval_output = SafeIntervalAndCapacityAccessService.Stringto3Array(SafeIntervalRaw);

            SafeIntervalEntity safeIntervalEntity = new SafeIntervalEntity(id, safeinterval_output);
            safeIntervalEntityList.add(safeIntervalEntity);
        }

        responseEntity = ResponseEntity.ok(MicroResponseUtils.success(safeIntervalEntityList));


        return responseEntity;
    }

    @GetMapping("/airlane/search")
    public ResponseEntity getAirlaneSearch(String index,Integer type) {

        return safeIntervalAndCapacityAccessService.getAirlaneCondition(index,type);
    }
    ///////////////////////// 航路信息Air Lane的增删改查、分页、容量评估、安全间隔查询  上方 //////////////////////////////////////




    ///////////////////////// 空域网格Spatial Grid的增删改查、分页  下方 //////////////////////////////////////
    @GetMapping("/spatialgrid/selectall")
    public ResponseEntity getSpatialgridAllUsers() {
        return ResponseEntity.ok(MicroResponseUtils.success(safeIntervalAndCapacityAccessService.findAllSpatialGrids()));
    }

    @GetMapping("/spatialgrid/select")
    public ResponseEntity getSpatialgridUserById(@RequestParam Long id) {
        Optional<SpatialGridEntity> user = safeIntervalAndCapacityAccessService.findSpatialGridsById(id);
        ResponseEntity responseEntity;
        if(user.isPresent()){
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(user.get()));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @PostMapping("/spatialgrid/create")
    public ResponseEntity createSpatialgridUser(@RequestBody SpatialGridEntity user) {
        SpatialGridEntity createdUser = safeIntervalAndCapacityAccessService.saveSpatialGrids(user);
        return ResponseEntity.ok(MicroResponseUtils.success(null));
    }

    @PostMapping("/spatialgrid/update")
    public ResponseEntity updateSpatialgridUser(@RequestBody SpatialGridEntity user) {
        Optional<SpatialGridEntity> update = safeIntervalAndCapacityAccessService.updateSpatialGrids(user);
        ResponseEntity responseEntity;
        if(update.isPresent()){
            responseEntity = new ResponseEntity<>(update.get(), HttpStatus.OK);
            //return ResponseEntity.ok(MicroResponseUtils.success(update.get()));
        }
        else{
            responseEntity = new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
            //return ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @PostMapping("/spatialgrid/delete")
    public ResponseEntity deleteSpatialgridUser(@RequestParam Long id) {
        //return userSysService.deleteAirLane(id) ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
        boolean isDeleted = safeIntervalAndCapacityAccessService.deleteSpatialGrids(id);
        return isDeleted ? ResponseEntity.ok(MicroResponseUtils.success("User deleted successfully")) : ResponseEntity.ok(MicroResponseUtils.error("User not found"));
    }
    ///////////////////////// 空域网格Spatial Grid的增删改查、分页  上方 //////////////////////////////////////



    ///////////////////////// 起降区Takeoff Land Aera的增删改查、分页、容量评估、安全间隔查询  下方 //////////////////////////////////
    @GetMapping("/takeoffland/selectall")
    public ResponseEntity getTakeofflandAllUsers() {
        return ResponseEntity.ok(MicroResponseUtils.success(safeIntervalAndCapacityAccessService.findAllTakeoffLand()));
    }

    @GetMapping("/takeoffland/select")
    public ResponseEntity getTakeofflandUserById(@RequestParam Long id) {
        Optional<TakeoffLandEntity> user = safeIntervalAndCapacityAccessService.findTakeoffLandById(id);
        ResponseEntity responseEntity;
        if(user.isPresent()){
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(user.get()));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @PostMapping("/takeoffland/create")
    public ResponseEntity createTakeofflandUser(@RequestBody TakeoffLandEntity user) {
        TakeoffLandEntity createdUser = safeIntervalAndCapacityAccessService.saveTakeoffLand(user);
        return ResponseEntity.ok(MicroResponseUtils.success(null));
    }

    @PostMapping("/takeoffland/update")
    public ResponseEntity updateTakeofflandUser(@RequestBody TakeoffLandEntity user) {
        Optional<TakeoffLandEntity> update = safeIntervalAndCapacityAccessService.updateTakeoffLand(user);
        ResponseEntity responseEntity;
        if(update.isPresent()){
            responseEntity = new ResponseEntity<>(update.get(), HttpStatus.OK);
            //return ResponseEntity.ok(MicroResponseUtils.success(update.get()));
        }
        else{
            responseEntity = new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
            //return ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @PostMapping("/takeoffland/delete")
    public ResponseEntity deleteTakeofflandUser(@RequestParam Long id) {
        //return userSysService.deleteAirLane(id) ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
        boolean isDeleted = safeIntervalAndCapacityAccessService.deleteTakeoffLand(id);
        return isDeleted ? ResponseEntity.ok(MicroResponseUtils.success("User deleted successfully")) : ResponseEntity.ok(MicroResponseUtils.error("User not found"));
    }

    @PostMapping("/takeoffland/capacityeval")
    public ResponseEntity TakeofflandCapacityEval(@RequestBody TakeoffLandEntity user) {
        // 调出user对象中的关键参数
        String dataSource_temp = user.getDataSource();
        Integer min15CapacityEvalResult_temp = user.getMin15CapacityEvalResult();
        Integer h1CapacityEvalResult_temp = user.getH1CapacityEvalResult();
        Integer staticCapacityEvalResult_temp = user.getStaticCapacityEvalResult();

        // 此处运用上述输入的关键参数，运行容量评估算法，得到最终要输入给前端的  capacityEvalResult
        CapacityEvalAlgorithm capacityEvalAlgorithm = new CapacityEvalAlgorithm();
        Integer[] capacityEvalResult = capacityEvalAlgorithm.capacityEval();
        // 得到待评估的容量评估值
        user.setMin15CapacityEvalResult(capacityEvalResult[0]);
        user.setH1CapacityEvalResult(capacityEvalResult[1]);
        user.setStaticCapacityEvalResult(capacityEvalResult[2]);
        // 将容量评估值和HTTP响应一同返回
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @GetMapping("/takeoffland/idsafeinterval")
    public ResponseEntity getTakeofflandSafeInterval(@RequestParam Long id) {
        Optional<TakeoffLandEntity> user = safeIntervalAndCapacityAccessService.findTakeoffLandById(id); // 获得某id的所有数据
        ResponseEntity responseEntity;
        if(user.isPresent()){
            String SafeIntervalRaw = user.get().getSafeIntervalRepo();    // 获得列表中的字符串
            Integer[][][] safeinterval_output = SafeIntervalAndCapacityAccessService.Stringto3Array(SafeIntervalRaw);
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(safeinterval_output));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @GetMapping("/takeoffland/idsafeintervalall")
    public ResponseEntity getTakeofflandSafeIntervalall() {
        List<TakeoffLandEntity> user = safeIntervalAndCapacityAccessService.findAllTakeoffLand(); // 获得某id的所有数据
        ResponseEntity responseEntity;
        List<SafeIntervalEntity> safeIntervalEntityList = new ArrayList<>();

        for (int i = 0; i < 20; i++){
            TakeoffLandEntity takeoffland = user.get(i);
            Integer id = takeoffland.getId();
            String SafeIntervalRaw = takeoffland.getSafeIntervalRepo();
            Integer[][][] safeinterval_output = SafeIntervalAndCapacityAccessService.Stringto3Array(SafeIntervalRaw);

            SafeIntervalEntity safeIntervalEntity = new SafeIntervalEntity(id, safeinterval_output);
            safeIntervalEntityList.add(safeIntervalEntity);
        }

        responseEntity = ResponseEntity.ok(MicroResponseUtils.success(safeIntervalEntityList));


        return responseEntity;
    }

    @GetMapping("/takeoffland/search")
    public ResponseEntity getTakeofflandSearch(String index,Integer type) {

        return safeIntervalAndCapacityAccessService.getTakeofflandCondition(index,type);
    }
    ///////////////////////// 起降区Takeoff Land Aera的增删改查、分页、容量评估、安全间隔查询  上方 //////////////////////////////////////



    ///////////////////////// 工作区Work Zone的增删改查、分页、容量评估、安全间隔查询  下方 //////////////////////////////////////
    @GetMapping("/workzone/selectall")
    public ResponseEntity getWorkzoneAllUsers() {
        return ResponseEntity.ok(MicroResponseUtils.success(safeIntervalAndCapacityAccessService.findAllWorkZone()));
    }

    @GetMapping("/workzone/select")
    public ResponseEntity getWorkzoneUserById(@RequestParam Long id) {
        Optional<WorkZoneEntity> user = safeIntervalAndCapacityAccessService.findWorkZoneById(id);
        ResponseEntity responseEntity;
        if(user.isPresent()){
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(user.get()));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @PostMapping("/workzone/create")
    public ResponseEntity createWorkzoneUser(@RequestBody WorkZoneEntity user) {
        WorkZoneEntity createdUser = safeIntervalAndCapacityAccessService.saveWorkZone(user);
        return ResponseEntity.ok(MicroResponseUtils.success(null));
    }

    @PostMapping("/workzone/update")
    public ResponseEntity updateWorkzoneUser(@RequestBody WorkZoneEntity user) {
        Optional<WorkZoneEntity> update = safeIntervalAndCapacityAccessService.updateWorkZone(user);
        ResponseEntity responseEntity;
        if(update.isPresent()){
            responseEntity = new ResponseEntity<>(update.get(), HttpStatus.OK);
            //return ResponseEntity.ok(MicroResponseUtils.success(update.get()));
        }
        else{
            responseEntity = new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
            //return ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @PostMapping("/workzone/delete")
    public ResponseEntity deleteWorkzoneUser(@RequestParam Long id) {
        //return userSysService.deleteAirLane(id) ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
        boolean isDeleted = safeIntervalAndCapacityAccessService.deleteWorkZone(id);
        return isDeleted ? ResponseEntity.ok(MicroResponseUtils.success("User deleted successfully")) : ResponseEntity.ok(MicroResponseUtils.error("User not found"));
    }

    @PostMapping("/workzone/capacityeval")
    public ResponseEntity WorkzoneCapacityEval(@RequestBody WorkZoneEntity user) {
        // 调出user对象中的关键参数
        String dataSource_temp = user.getDataSource();
        Integer min15CapacityEvalResult_temp = user.getMin15CapacityEvalResult();
        Integer h1CapacityEvalResult_temp = user.getH1CapacityEvalResult();
        Integer staticCapacityEvalResult_temp = user.getStaticCapacityEvalResult();

        // 此处运用上述输入的关键参数，运行容量评估算法，得到最终要输入给前端的  capacityEvalResult
        CapacityEvalAlgorithm capacityEvalAlgorithm = new CapacityEvalAlgorithm();
        Integer[] capacityEvalResult = capacityEvalAlgorithm.capacityEval();
        // 得到最终的容量评估值
        user.setMin15CapacityEvalResult(capacityEvalResult[0]);
        user.setH1CapacityEvalResult(capacityEvalResult[1]);
        user.setStaticCapacityEvalResult(capacityEvalResult[2]);
        // 将容量评估值和HTTP响应一同返回
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @GetMapping("/workzone/idsafeinterval")
    public ResponseEntity getWorkzoneSafeInterval(@RequestParam Long id) {
        Optional<WorkZoneEntity> user = safeIntervalAndCapacityAccessService.findWorkZoneById(id); // 获得某id的所有数据
        ResponseEntity responseEntity;
        if(user.isPresent()){
            String SafeIntervalRaw = user.get().getSafeIntervalRepo();    // 获得列表中的字符串
            Integer[][][] safeinterval_output = SafeIntervalAndCapacityAccessService.Stringto3Array(SafeIntervalRaw);
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(safeinterval_output));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @GetMapping("/workzone/search")
    public ResponseEntity getWorkzoneSearch(String index,Integer type) {

        return safeIntervalAndCapacityAccessService.getWorkzoneCondition(index,type);
    }
    ///////////////////////// 工作区Work Zone的增删改查、分页、容量评估、安全间隔查询  上方 //////////////////////////////////////

    @GetMapping("/workzone/idsafeintervalall")
    public ResponseEntity getWorkzoneSafeIntervalall() {
        List<WorkZoneEntity> user = safeIntervalAndCapacityAccessService.findAllWorkZone(); // 获得某id的所有数据
        ResponseEntity responseEntity;
        List<SafeIntervalEntity> safeIntervalEntityList = new ArrayList<>();

        for (int i = 0; i < 20; i++){
            WorkZoneEntity workZone = user.get(i);
            Integer id = workZone.getId();
            String SafeIntervalRaw = workZone.getSafeIntervalRepo();
            Integer[][][] safeinterval_output = SafeIntervalAndCapacityAccessService.Stringto3Array(SafeIntervalRaw);

            SafeIntervalEntity safeIntervalEntity = new SafeIntervalEntity(id, safeinterval_output);
            safeIntervalEntityList.add(safeIntervalEntity);
        }

        responseEntity = ResponseEntity.ok(MicroResponseUtils.success(safeIntervalEntityList));


        return responseEntity;
    }

}
